package com.fr.web.core.service;

import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.core.BaseCoreUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.FRCoreContext;
import com.fr.base.core.RSADecode;
import com.fr.base.core.html.Tag;
import com.fr.base.core.html.TextHtml;
import com.fr.base.core.json.JSONObject;
import com.fr.report.FineBook;
import com.fr.report.ReportPage;
import com.fr.report.ReportPageAttr;
import com.fr.report.io.HTMLExportAttr;
import com.fr.report.io.ReportExportAttr;
import com.fr.util.Consts;
import com.fr.util.Utils;
import com.fr.web.Browser;
import com.fr.web.Repository;
import com.fr.web.core.Service;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.TreeHTMLConverter;
import com.fr.web.core.WebUtils;
import com.fr.web.core.chwriter.ViewCellWriter;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ViewService
  implements Service
{
  public static final String CUSTOM_SHEET_CONTAINER_CLASS = "fr_view_container_class";
  private static ViewService SC = new ViewService();
  private static boolean registed = false;

  public static ViewService getInstance()
  {
    return SC;
  }

  public boolean accept(String paramString)
  {
    return paramString.startsWith("view_");
  }

  public void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, String paramString1, String paramString2)
    throws Exception
  {
    if ("view_content".equals(paramString1))
      readPageContent(paramString2, paramHttpServletRequest, paramHttpServletResponse);
  }

  private static void readPageContent(String paramString, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    int i = 0;
    int j = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "reportIndex"));
    String str = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "pn");
    if (str != null)
    {
      localObject1 = Utils.string2Number(str);
      if (localObject1 != null)
        if ((i = ((Number)localObject1).intValue()) >= 0)
          break label51;
      i = 2147483647;
    }
    label51: Object localObject1 = SessionDealWith.getSessionIDInfor(paramString);
    if (localObject1 == null)
      return;
    ReportPage localReportPage = ((SessionIDInfor)localObject1).getReportPage(j, i);
    if (localReportPage == null)
      return;
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    int k = localReportPage.getCurrentPageNumber();
    int l = localReportPage.getTotalPages();
    new Tag("script").sub(new TextHtml("FR._p=FR.SessionMgr.get(\"" + paramString + "\");" + "FR._p.lgps[" + j + "].currentPageIndex=" + k + ";" + "FR._p.lgps[" + j + "].reportTotalPage=" + l + ";" + "delete FR._p;")).writeHtml(localPrintWriter);
    Tag localTag = new Tag("div").cls("fr_view_container_class");
    localTag.css("cursor", "default").cls("sheet-container");
    Repository localRepository = new Repository(paramHttpServletRequest, (SessionIDInfor)localObject1);
    ColumnRow localColumnRow = (localReportPage.getReportPageAttr() == null) ? null : localReportPage.getReportPageAttr().getFreezeColumnRow();
    FRContext.getLogger().info(Inter.getLocText("INFO-Start_Converting_ReportPage_To_Html"));
    long l1 = System.currentTimeMillis();
    ViewCellWriter localViewCellWriter = new ViewCellWriter(localRepository, j);
    if (localRepository.getBrowser().shouldConsiderHeavyTD())
    {
      localObject2 = ((SessionIDInfor)localObject1).getContextBook().getReportExportAttr();
      if ((localObject2 != null) && (((ReportExportAttr)localObject2).getHtmlExportAttr() != null))
      {
        HTMLExportAttr localHTMLExportAttr = ((ReportExportAttr)localObject2).getHtmlExportAttr();
        if (localHTMLExportAttr.isTDHeavy())
          localViewCellWriter.setHeavy(true);
      }
    }
    Object localObject2 = new TreeHTMLConverter(localReportPage, localViewCellWriter, localColumnRow, localRepository, localTag, false);
    ((TreeHTMLConverter)localObject2).convert();
    localTag.writeHtml(localPrintWriter);
    SessionDealWith.writeRegistionDiv(paramHttpServletRequest, localPrintWriter);
    localPrintWriter.flush();
    localPrintWriter.close();
    long l2 = System.currentTimeMillis();
    FRContext.getLogger().info(Inter.getLocText("INFO-End_Converting_ReportPage_To_Html") + "\t" + Inter.getLocText("INFO-Time_Taken") + (l2 - l1) + Inter.getLocText("Milliseconds"));
  }

  static
  {
    byte[] arrayOfByte = FRCoreContext.getBytes();
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      RSADecode.decode(arrayOfByte, localByteArrayOutputStream);
    }
    catch (Exception localException1)
    {
    }
    try
    {
      JSONObject localJSONObject = new JSONObject(new String(localByteArrayOutputStream.toByteArray(), "UTF-8"));
      registed = (localJSONObject.getLong("deadline") > Calendar.getInstance().getTimeInMillis()) && (localJSONObject.has("version")) && (ComparatorUtils.equals(Consts.VERSION, localJSONObject.getString("version"))) && (((localJSONObject.getLong("deadline") - Calendar.getInstance().getTimeInMillis() < 3153600000L) || ((localJSONObject.has("macaddress")) && (BaseCoreUtils.getMacAddresses().contains(localJSONObject.getString("macaddress"))))));
    }
    catch (Exception localException2)
    {
    }
  }
}